package com.dicsystems.myrec;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.R.drawable;

public class Common {

	public Context me;
	public Calendar now = Calendar.getInstance();
	public static final SimpleDateFormat DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static final SimpleDateFormat DATE = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat YEAR = new SimpleDateFormat("yyyy");
	public static final SimpleDateFormat MONTH = new SimpleDateFormat("MM");
	public static final SimpleDateFormat DAY = new SimpleDateFormat("dd");
	public static final SimpleDateFormat TIME = new SimpleDateFormat("HH:mm");
	public static final SimpleDateFormat HOUR = new SimpleDateFormat("HH");
	public static final SimpleDateFormat MINUTE = new SimpleDateFormat("mm");

	public Common() {}

	public Common(Context context) {
		this.me = context;
	}

	public String read(String file) {
		StringBuilder sb = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(me.getResources().getAssets().open(file), "SJIS"));
			for(String text; (text = br.readLine()) != null;) {
				sb.append(text + "\n");
			}
			br.close();
		}
		catch(IOException e) {}
		return sb.toString();
	}

	public String tag(TextView text) {
		return (String)text.getTag().toString().trim();
	}

	public String text(TextView text) {
		return text.getText().toString().trim();
	}

	public Calendar calendar(Button button) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date(text(button)));
		return calendar;
	}

	public String getDate(DatePicker picker) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
		return date(calendar);
	}

	public Calendar today() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		return calendar;
	}

	public Date datetime(String value) {
		try {
			return DATETIME.parse(value);
		}
		catch(ParseException e) {
			return null;
		}
	}

	public Date date(String value) {
		try {
			return DATE.parse(value);
		}
		catch(ParseException e) {
			return null;
		}
	}

	public Date time(String value) {
		try {
			return TIME.parse(value);
		}
		catch(Exception e) {
			return null;
		}
	}

	public String date() {
		return DATE.format(now.getTime());
	}

	public String datetime(Calendar calendar) {
		return DATETIME.format(calendar.getTime());
	}

	public String date(Calendar calendar) {
		return DATE.format(calendar.getTime());
	}

	public String time(Calendar calendar) {
		return TIME.format(calendar.getTime());
	}

	public int year() {
		return now.get(Calendar.YEAR);
	}

	public int month() {
		return now.get(Calendar.MONTH) + 1;
	}

	public int day() {
		return now.get(Calendar.DAY_OF_MONTH);
	}

	public int hour() {
		return now.get(Calendar.HOUR_OF_DAY);
	}

	public int minute() {
		return now.get(Calendar.MINUTE);
	}

	public int year(TextView view) {
		return Integer.parseInt(YEAR.format(date(view.getText().toString())));
	}

	public int month(TextView view) {
		return Integer.parseInt(MONTH.format(date(view.getText().toString())));
	}

	public int day(TextView view) {
		return Integer.parseInt(DAY.format(date(view.getText().toString())));
	}

	public int hour(TextView view) {
		return Integer.parseInt(HOUR.format(time(view.getText().toString())));
	}

	public int minute(TextView view) {
		return Integer.parseInt(MINUTE.format(time(view.getText().toString())));
	}

	public void error(int message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(me);
		builder.setIcon(drawable.ic_dialog_alert);
		builder.setTitle(R.string.error);
		builder.setMessage(message);
		builder.setPositiveButton(R.string.ok, null);
		builder.show();
	}

	/*public List<Bitmap> getSd() {
		List<Bitmap> image = new ArrayList<Bitmap>();
		Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
		if(cursor.moveToFirst()) {
			do {
				image.add(MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), cursor.getLong(cursor.getColumnIndexOrThrow("_id")), MediaStore.Images.Thumbnails.MICRO_KIND, null));
				//MediaStore.Images.Media.getContentUri(null);
			} while(cursor.moveToNext());
		}
		cursor.close();
		return image;
	}*/
}
